package com.example.camunda;


import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * CamundaApplication
 */
@SpringBootApplication
@RestController
@RequestMapping
public class CamundaApplication {
    private final RuntimeService runtimeService;
    private final RepositoryService repositoryService;


    public CamundaApplication(RuntimeService runtimeService, RepositoryService repositoryService) {
        this.runtimeService = runtimeService;
        this.repositoryService = repositoryService;
    }


    /**
     * 启动流程.
     *
     * @param key 流程key
     * @return
     */
    public String start(String key) {
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey(key).latestVersion().singleResult();
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
        return key + "流程启动成功,流程实例ID为:" + processInstance.getId();
    }


    public static void main(String[] args) {
        SpringApplication.run(CamundaApplication.class, args);
    }

}
